package game;

import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;

@SuppressWarnings("serial")
public class ScoreList extends JFrame {

	/**
	 * @param args
	 */
	ArrayList<Player> players;
//	Player me;
	private ArrayList<JLabel> labels = new ArrayList<JLabel>();
	private JList playerList;

	/**
	 * 
	 * @param players
	 */
	public ScoreList(ArrayList<Player> players) {
		super("TKgame v. 1.0");
		this.players = players;
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setLocation(600,100);
		this.setSize(300, 500);
		this.setResizable(true);
		this.setLayout(new GridLayout(20, 20, 0, 0));
		this.setVisible(true);

		draw();
		this.setAlwaysOnTop(true);
	}
	
	/**
	 * 
	 */
	public void draw() {
		for (int j = 0; j < players.size(); j++) {
			JLabel l = new JLabel(players.get(j).toString());
			l.setSize(200, 200);
			this.add(l);
			labels.add(l);
		}	
		this.pack();
	}
	
	/**
	 * 
	 * @param p
	 */
	public void updateScoreOnScreen(Player p) {
		int playerno = players.indexOf(p);
		labels.get((playerno)).setText(players.get(playerno).toString());
	}
	


	public synchronized void update(ArrayList<Player> players) {

		this.players = players;
		while (labels.size()<players.size()){
			JLabel l = new JLabel("New label");
			l.setSize(200, 200);
			this.add(l);
			labels.add(l);
		}
		this.pack();
		
		for (Player p: players){
			updateScoreOnScreen(p);
		}

	}
}	
	
